import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        isLogin: false,
      },
    },
    {
      path: '/login',
      name: 'login-or-reg',
      component: () => import('../views/LoginOrReg.vue'),
      meta: {
        isLogin: false,
      },
    },
    {
      path: '/pub',
      name: 'pub',
      component: () => import('../views/PubHomeWork.vue'),
      meta: {
        isLogin: true,
      },
    },
  ],
})

router.beforeEach((to, from) => {
  let token = localStorage.getItem('homework-token')
  if (token === null && to.meta.isLogin === true) {
    return '/login'
  }
})

export default router
